package cn.eleven.gui;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URI;


public class ChatClient extends JFrame {
    private JTextArea chatArea;
    private JTextField inputField;
    private JTextField recipientField;
    private WebSocketClient client;
    private String username;

    public ChatClient(String username) {
        this.username = username;
        setTitle("Chat - " + username);
        setSize(400, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        chatArea = new JTextArea();
        chatArea.setEditable(false);
        add(new JScrollPane(chatArea), BorderLayout.CENTER);

        JPanel panel = new JPanel(new BorderLayout());
        recipientField = new JTextField("接收者用户名");
        inputField = new JTextField();
        JButton sendButton = new JButton("发送");

        panel.add(recipientField, BorderLayout.NORTH);
        panel.add(inputField, BorderLayout.CENTER);
        panel.add(sendButton, BorderLayout.EAST);
        add(panel, BorderLayout.SOUTH);

        sendButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                sendMessage();
            }
        });

        connectToServer();
    }

    private void connectToServer() {
        try {
            client = new WebSocketClient(new URI("ws://localhost:8080/?username=" + username)) {
                @Override
                public void onOpen(ServerHandshake handshakedata) {
                    chatArea.append("连接服务器成功\n");
                }

                @Override
                public void onMessage(String message) {
                    chatArea.append(message + "\n");
                }

                @Override
                public void onClose(int code, String reason, boolean remote) {
                    chatArea.append("断开连接\n");
                }

                @Override
                public void onError(Exception ex) {
                    chatArea.append("错误：" + ex.getMessage() + "\n");
                }
            };
            client.connect();
        } catch (Exception e) {
            chatArea.append("连接错误：" + e.getMessage() + "\n");
        }
    }

    private void sendMessage() {
        String recipient = recipientField.getText();
        String message = inputField.getText();
        if (!recipient.isEmpty() && !message.isEmpty()) {
            client.send(recipient + "|" + message);
            inputField.setText("");
        }
    }

    public static void main(String[] args) {
        String username = JOptionPane.showInputDialog("请输入用户名：");
        if (username != null && !username.trim().isEmpty()) {
            SwingUtilities.invokeLater(() -> new ChatClient(username).setVisible(true));
        }
    }
}
